package 富婆梦.第一阶段.第八周Scoket.聊天程序;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第八周Scoket
 * @ClassName: SimpleChatClientA
 * @Author: hemengjin
 * @Description: 实现内容：聊天对话框，客户端A，支持多客户连接，可以在控制台发送信息，接受信息
 * 方法：
 * 1、getConnection（）：建立Socket连接，并使用两个线程同时进行接受信息和发送信息
 * 2、readMessage（）：读取服务端发来的信息
 * 3、sendMessage（）：发送信息给服务端
 * 4、closeConnection（）：断开Scoket连接
 * @Date:2021/9/22 10:19
 * @Version: 1.0
 */
public class SimpleChatClientA {
    PrintWriter writer;
    BufferedReader reader;
    Socket socket;


    //建立Scoket连接
    private void getConnection() {
        try {
            socket = new Socket("localhost", 8888);
            System.out.println("成功连接到服务器！");
            System.out.println("可在控制台输入你想给服务器说的话！");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //监听键盘输入
        Thread readThread = new Thread(new IncomingReader());
        readThread.start();
        //监听服务端发送的信息
        Thread writeThread = new Thread(new OutcomingWriter());
        writeThread.start();
    }

    //读取服务端发来的信息
    private void readMessage() {
        String message;
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
            reader = new BufferedReader(inputStreamReader);
            while ((message = reader.readLine()) != null) {
                System.out.println("读到了服务器的信息：" + message);
            }
        } catch (Exception e) {
            closeConnection();
            //e.printStackTrace();
        }
    }

    //发送信息给服务端
    private void sendMessage() {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try {
            writer = new PrintWriter(socket.getOutputStream());
            writer.println(s);
            writer.flush();
        } catch (Exception e) {
            closeConnection();
            //e.printStackTrace();
        }
    }

    //断开Scoket连接
    private void closeConnection() {
        try {
            writer.close();
            reader.close();
            socket.close();
            System.out.println(socket.isClosed());
            System.out.println("连接被关闭！");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //监听接收服务端信息的线程类
    private class IncomingReader implements Runnable {
        public void run() {
            while (true) {
                if (socket.isClosed() == true) {
                    return;
                } else {
                    readMessage();
                }
            }
        }
    }

    //监听发送信息的线程类
    private class OutcomingWriter implements Runnable {
        public void run() {
            while (true) {
                if (socket.isClosed() == true) {
                    return;
                } else {
                    sendMessage();
                }
            }
        }
    }

    public static void main(String[] args) {
        SimpleChatClientA clientA = new SimpleChatClientA();
        //连接服务端
        clientA.getConnection();
    }

}

